import type { ConfigEnv, UserConfig } from 'vite'

import { defineConfig, mergeConfig } from 'vite'
import { defineBuildConfig } from './build'
import { envDir } from './paths'
import { definePluginsConfig } from './plugins'

// https://vite.dev/config/
export function defineViteConfig(
  srcDir: string,
  customConfig: UserConfig = {},
) {
  return defineConfig(({ command }: ConfigEnv) => {
    const defaultConfig: UserConfig = {
      envDir,
      resolve: {
        alias: { '@': srcDir },
      },
      server: { host: true, port: 1520 },
      plugins: definePluginsConfig(command),
      build: defineBuildConfig(),
      css: {
        preprocessorOptions: {
          less: {
            modifyVars: {
              'arcoblue-1': '#E8FFF1',
              'arcoblue-2': '#ADEDC8',
              'arcoblue-3': '#78DBA5',
              'arcoblue-4': '#49CA87',
              'arcoblue-5': '#21B86F',
              'arcoblue-6': '#00A65B',
              'arcoblue-7': '#009054',
              'arcoblue-8': '#00794B',
              'arcoblue-9': '#006340',
              'arcoblue-10': '#004D34',
            },
            javascriptEnabled: true,
          },
        },
      },
    }
    return mergeConfig(defaultConfig, customConfig || {})
  })
}
